package com.getpebble.android.core.provider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.model.LanguagePackModel;
import com.getpebble.android.common.model.PblAndroidAppModel;
import com.getpebble.android.common.model.PblDataModel;
import com.getpebble.android.common.model.PblDeviceJoinManifestModel;
import com.getpebble.android.common.model.PblDeviceModel;
import com.getpebble.android.common.model.PblFirmwareManifestModel;
import com.getpebble.android.common.model.PblInstalledAppDataModel;
import com.getpebble.android.common.model.PblNotificationModel;
import com.getpebble.android.common.model.PreferenceModel;
import com.getpebble.android.common.provider.model.PblInstalledJoinLockerAppDataModel;
import com.getpebble.android.common.provider.model.PblLockerAppDataModel;
import com.getpebble.android.config.model.BootConfigDataModel;
import com.google.common.collect.ImmutableMap;
import com.sun.mail.iap.Response;
import java.util.Map;

public class PblContentProvider extends ContentProvider {
    public static final Uri CONTENT_URI = Uri.parse("content://com.getpebble.android.internal.provider");
    public static final String TAG = PblContentProvider.class.getSimpleName();
    private static Map<Integer, String> sCodeToPathMap = ImmutableMap.builder().put(Integer.valueOf(1), "locker_apps").put(Integer.valueOf(2), "boot_config").put(Integer.valueOf(3), "devices").put(Integer.valueOf(4), "manifests").put(Integer.valueOf(5), PblDeviceJoinManifestModel.getJoinedTableName()).put(Integer.valueOf(6), "installed_apps").put(Integer.valueOf(7), PblInstalledJoinLockerAppDataModel.getJoinedTableName()).put(Integer.valueOf(8), "preferences").put(Integer.valueOf(9), "notifications").put(Integer.valueOf(10), "android_apps").put(Integer.valueOf(11), "pebble_language_packs").put(Integer.valueOf(12), LanguagePackModel.getRawQueryTableName()).build();
    private static final UriMatcher sUriMatcher = new UriMatcher(-1);
    private SQLiteDatabase mSQLiteDatabase = null;

    private class DatabaseHelper extends SQLiteOpenHelper {

        private class PblContentProviderMigrationException extends RuntimeException {
            public PblContentProviderMigrationException(int oldVersion, int newVersion, Exception e) {
                super(String.format("Failed to upgrade database from %d to %d", new Object[]{Integer.valueOf(oldVersion), Integer.valueOf(newVersion)}), e);
            }
        }

        public DatabaseHelper(Context context) {
            super(context, "pebble", null, 12);
        }

        public void onCreate(SQLiteDatabase db) {
            PblDataModel watchAppTable = new PblLockerAppDataModel();
            db.execSQL(watchAppTable.getCreateTableCommand());
            db.execSQL(watchAppTable.getCreateTriggerCommand());
            PblDataModel deviceStateTable = new PblDeviceModel();
            db.execSQL(deviceStateTable.getCreateTableCommand());
            db.execSQL(deviceStateTable.getCreateTriggerCommand());
            PblDataModel manifestTable = new PblFirmwareManifestModel();
            db.execSQL(manifestTable.getCreateTableCommand());
            db.execSQL(manifestTable.getCreateTriggerCommand());
            PblDataModel installAppTable = new PblInstalledAppDataModel();
            db.execSQL(installAppTable.getCreateTableCommand());
            db.execSQL(installAppTable.getCreateTriggerCommand());
            db.execSQL(new BootConfigDataModel().getCreateTableCommand());
            db.execSQL(new PreferenceModel().getCreateTableCommand());
            db.execSQL(new PblNotificationModel().getCreateTableCommand());
            db.execSQL(new LanguagePackModel().getCreateTableCommand());
            db.execSQL(new PblAndroidAppModel().getCreateTableCommand());
        }

        /* JADX WARNING: inconsistent code. */
        /* Code decompiled incorrectly, please refer to instructions dump. */
        public void onUpgrade(android.database.sqlite.SQLiteDatabase r9, int r10, int r11) {
            /*
            r8 = this;
            r4 = com.getpebble.android.core.provider.PblContentProvider.TAG;
            r5 = new java.lang.StringBuilder;
            r5.<init>();
            r6 = "Upgrading database from version ";
            r5 = r5.append(r6);
            r5 = r5.append(r10);
            r6 = " to ";
            r5 = r5.append(r6);
            r5 = r5.append(r11);
            r6 = ":";
            r5 = r5.append(r6);
            r5 = r5.toString();
            com.getpebble.android.common.core.trace.Trace.debug(r4, r5);
            switch(r10) {
                case 0: goto L_0x0044;
                case 1: goto L_0x0044;
                case 2: goto L_0x0044;
                case 3: goto L_0x0044;
                case 4: goto L_0x0050;
                case 5: goto L_0x0050;
                case 6: goto L_0x0050;
                case 7: goto L_0x005c;
                case 8: goto L_0x008b;
                case 9: goto L_0x00af;
                case 10: goto L_0x00cb;
                case 11: goto L_0x00dd;
                default: goto L_0x002b;
            };
        L_0x002b:
            r4 = com.getpebble.android.core.provider.PblContentProvider.TAG;	 Catch:{ Exception -> 0x007d }
            r5 = new java.lang.StringBuilder;	 Catch:{ Exception -> 0x007d }
            r5.<init>();	 Catch:{ Exception -> 0x007d }
            r6 = "**** Unable to upgrade from version ";
            r5 = r5.append(r6);	 Catch:{ Exception -> 0x007d }
            r5 = r5.append(r10);	 Catch:{ Exception -> 0x007d }
            r5 = r5.toString();	 Catch:{ Exception -> 0x007d }
            com.getpebble.android.common.core.trace.Trace.error(r4, r5);	 Catch:{ Exception -> 0x007d }
        L_0x0043:
            return;
        L_0x0044:
            r4 = new com.getpebble.android.common.model.PblNotificationModel;	 Catch:{ Exception -> 0x007d }
            r4.<init>();	 Catch:{ Exception -> 0x007d }
            r4 = r4.getCreateTableCommand();	 Catch:{ Exception -> 0x007d }
            r9.execSQL(r4);	 Catch:{ Exception -> 0x007d }
        L_0x0050:
            r4 = new com.getpebble.android.common.model.PblAndroidAppModel;	 Catch:{ Exception -> 0x007d }
            r4.<init>();	 Catch:{ Exception -> 0x007d }
            r4 = r4.getCreateTableCommand();	 Catch:{ Exception -> 0x007d }
            r9.execSQL(r4);	 Catch:{ Exception -> 0x007d }
        L_0x005c:
            r4 = "notification_parsing";
            r4 = com.getpebble.android.util.SQLUtil.getDropTableCommand(r4);	 Catch:{ Exception -> 0x007d }
            r9.execSQL(r4);	 Catch:{ Exception -> 0x007d }
            r4 = com.getpebble.android.common.model.PblNotificationModel.addPropertyColumnCommands();	 Catch:{ Exception -> 0x007d }
            r2 = r4.iterator();	 Catch:{ Exception -> 0x007d }
        L_0x006d:
            r4 = r2.hasNext();	 Catch:{ Exception -> 0x007d }
            if (r4 == 0) goto L_0x008b;
        L_0x0073:
            r0 = r2.next();	 Catch:{ Exception -> 0x007d }
            r0 = (java.lang.String) r0;	 Catch:{ Exception -> 0x007d }
            r9.execSQL(r0);	 Catch:{ Exception -> 0x007d }
            goto L_0x006d;
        L_0x007d:
            r1 = move-exception;
            r4 = com.getpebble.android.core.provider.PblContentProvider.TAG;
            r5 = "Failed to upgrade database!";
            com.getpebble.android.common.core.trace.Trace.error(r4, r5, r1);
            r4 = new com.getpebble.android.core.provider.PblContentProvider$DatabaseHelper$PblContentProviderMigrationException;
            r4.<init>(r10, r11, r1);
            throw r4;
        L_0x008b:
            r4 = new com.getpebble.android.common.model.LanguagePackModel;	 Catch:{ Exception -> 0x007d }
            r4.<init>();	 Catch:{ Exception -> 0x007d }
            r4 = r4.getCreateTableCommand();	 Catch:{ Exception -> 0x007d }
            r9.execSQL(r4);	 Catch:{ Exception -> 0x007d }
            r4 = com.getpebble.android.common.model.PblDeviceModel.addLanguageColumnsCommands();	 Catch:{ Exception -> 0x007d }
            r2 = r4.iterator();	 Catch:{ Exception -> 0x007d }
        L_0x009f:
            r4 = r2.hasNext();	 Catch:{ Exception -> 0x007d }
            if (r4 == 0) goto L_0x00af;
        L_0x00a5:
            r0 = r2.next();	 Catch:{ Exception -> 0x007d }
            r0 = (java.lang.String) r0;	 Catch:{ Exception -> 0x007d }
            r9.execSQL(r0);	 Catch:{ Exception -> 0x007d }
            goto L_0x009f;
        L_0x00af:
            r4 = 9;
            if (r10 >= r4) goto L_0x00f5;
        L_0x00b3:
            r3 = 1;
        L_0x00b4:
            if (r3 != 0) goto L_0x00cb;
        L_0x00b6:
            r4 = "pebble_language_packs";
            r4 = com.getpebble.android.util.SQLUtil.getDropTableCommand(r4);	 Catch:{ Exception -> 0x007d }
            r9.execSQL(r4);	 Catch:{ Exception -> 0x007d }
            r4 = new com.getpebble.android.common.model.LanguagePackModel;	 Catch:{ Exception -> 0x007d }
            r4.<init>();	 Catch:{ Exception -> 0x007d }
            r4 = r4.getCreateTableCommand();	 Catch:{ Exception -> 0x007d }
            r9.execSQL(r4);	 Catch:{ Exception -> 0x007d }
        L_0x00cb:
            r4 = "devices";
            r5 = new com.getpebble.android.common.model.PblDataModel$Column;	 Catch:{ Exception -> 0x007d }
            r6 = com.getpebble.android.common.model.PblDataModel.Column.Type.TIMESTAMP;	 Catch:{ Exception -> 0x007d }
            r7 = "fw_timestamp";
            r5.<init>(r6, r7);	 Catch:{ Exception -> 0x007d }
            r4 = com.getpebble.android.util.SQLUtil.getAddColumnCommand(r4, r5);	 Catch:{ Exception -> 0x007d }
            r9.execSQL(r4);	 Catch:{ Exception -> 0x007d }
        L_0x00dd:
            r4 = com.getpebble.android.common.model.PblNotificationModel.addRemovedColumnCommands();	 Catch:{ Exception -> 0x007d }
            r2 = r4.iterator();	 Catch:{ Exception -> 0x007d }
        L_0x00e5:
            r4 = r2.hasNext();	 Catch:{ Exception -> 0x007d }
            if (r4 == 0) goto L_0x00f7;
        L_0x00eb:
            r0 = r2.next();	 Catch:{ Exception -> 0x007d }
            r0 = (java.lang.String) r0;	 Catch:{ Exception -> 0x007d }
            r9.execSQL(r0);	 Catch:{ Exception -> 0x007d }
            goto L_0x00e5;
        L_0x00f5:
            r3 = 0;
            goto L_0x00b4;
        L_0x00f7:
            r4 = com.getpebble.android.core.provider.PblContentProvider.TAG;	 Catch:{ Exception -> 0x007d }
            r5 = new java.lang.StringBuilder;	 Catch:{ Exception -> 0x007d }
            r5.<init>();	 Catch:{ Exception -> 0x007d }
            r6 = "Finished upgrading; db version = ";
            r5 = r5.append(r6);	 Catch:{ Exception -> 0x007d }
            r5 = r5.append(r11);	 Catch:{ Exception -> 0x007d }
            r5 = r5.toString();	 Catch:{ Exception -> 0x007d }
            com.getpebble.android.common.core.trace.Trace.debug(r4, r5);	 Catch:{ Exception -> 0x007d }
            goto L_0x0043;
            */
            throw new UnsupportedOperationException("Method not decompiled: com.getpebble.android.core.provider.PblContentProvider.DatabaseHelper.onUpgrade(android.database.sqlite.SQLiteDatabase, int, int):void");
        }
    }

    static {
        sUriMatcher.addURI("com.getpebble.android.internal.provider", "locker_apps", 1);
        sUriMatcher.addURI("com.getpebble.android.internal.provider", "boot_config", 2);
        sUriMatcher.addURI("com.getpebble.android.internal.provider", "devices", 3);
        sUriMatcher.addURI("com.getpebble.android.internal.provider", "manifests", 4);
        sUriMatcher.addURI("com.getpebble.android.internal.provider", PblDeviceJoinManifestModel.getJoinedTableName(), 5);
        sUriMatcher.addURI("com.getpebble.android.internal.provider", "installed_apps", 6);
        sUriMatcher.addURI("com.getpebble.android.internal.provider", PblInstalledJoinLockerAppDataModel.getJoinedTableName(), 7);
        sUriMatcher.addURI("com.getpebble.android.internal.provider", "preferences", 8);
        sUriMatcher.addURI("com.getpebble.android.internal.provider", "notifications", 9);
        sUriMatcher.addURI("com.getpebble.android.internal.provider", "android_apps", 10);
        sUriMatcher.addURI("com.getpebble.android.internal.provider", "pebble_language_packs", 11);
        sUriMatcher.addURI("com.getpebble.android.internal.provider", LanguagePackModel.getRawQueryTableName(), 12);
    }

    public boolean onCreate() {
        Trace.verbose(TAG, "onCreate()");
        if (getContext() == null) {
            return false;
        }
        this.mSQLiteDatabase = new DatabaseHelper(getContext()).getWritableDatabase();
        if (this.mSQLiteDatabase != null) {
            return true;
        }
        return false;
    }

    public String getType(Uri uri) throws IllegalArgumentException {
        sUriMatcher.match(uri);
        throw new IllegalArgumentException("Unsupported URI: " + uri);
    }

    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) throws IllegalArgumentException {
        String customRawQuery = getCustomRawQuery(uri, projection, selection, selectionArgs, sortOrder);
        if (customRawQuery != null) {
            Trace.debug(TAG, "Executing custom raw query: " + customRawQuery);
            return this.mSQLiteDatabase.rawQuery(customRawQuery, null);
        }
        String tableName = getTableNameFromUri(uri);
        SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
        queryBuilder.setTables(tableName);
        Cursor c = queryBuilder.query(this.mSQLiteDatabase, projection, selection, selectionArgs, null, null, sortOrder);
        if (c == null) {
            return c;
        }
        Context context = getContext();
        if (context == null) {
            return c;
        }
        c.setNotificationUri(context.getContentResolver(), uri);
        return c;
    }

    private String getCustomRawQuery(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        switch (sUriMatcher.match(uri)) {
            case Response.BAD /*12*/:
                return LanguagePackModel.getRawQuery(selectionArgs);
            default:
                return null;
        }
    }

    public Uri insert(Uri uri, ContentValues values) throws SQLiteException {
        return insert(uri, values, true);
    }

    private Uri insert(Uri uri, ContentValues values, boolean notify) throws SQLiteException {
        long rowId = this.mSQLiteDatabase.insert(getTableNameFromUri(uri), null, values);
        if (rowId > 0) {
            Uri newUri = ContentUris.withAppendedId(CONTENT_URI, rowId);
            notifyChange(uri);
            return newUri;
        }
        throw new SQLiteException("Unable to add values for " + uri.toString());
    }

    public int bulkInsert(Uri uri, ContentValues[] values) {
        int numValues = values.length;
        for (ContentValues value : values) {
            try {
                insert(uri, value, false);
            } catch (SQLiteException sqle) {
                numValues--;
                Trace.error(TAG, "Failed to insert!", sqle);
            }
        }
        notifyChange(uri);
        return numValues;
    }

    public int delete(Uri uri, String selection, String[] selectionArgs) throws IllegalArgumentException {
        int count = 0;
        try {
            count = this.mSQLiteDatabase.delete(getTableNameFromUri(uri), selection, selectionArgs);
        } catch (Exception e) {
            Trace.error(getClass().getSimpleName(), "Error performing delete query!", e);
        }
        notifyChange(uri);
        return count;
    }

    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        int count = 0;
        try {
            count = this.mSQLiteDatabase.update(getTableNameFromUri(uri), values, selection, selectionArgs);
        } catch (Exception e) {
            Trace.error(getClass().getSimpleName(), "Error performing update query!", e);
        }
        notifyChange(uri);
        return count;
    }

    private String getTableNameFromUri(Uri uri) throws IllegalArgumentException {
        int code = sUriMatcher.match(uri);
        if (sCodeToPathMap.containsKey(Integer.valueOf(code))) {
            return (String) sCodeToPathMap.get(Integer.valueOf(code));
        }
        throw new IllegalArgumentException("Unknown URI: " + uri);
    }

    private void notifyChange(Uri uri) {
        Context context = getContext();
        if (context != null) {
            context.getContentResolver().notifyChange(uri, null);
            if (uri.equals(PblInstalledAppDataModel.TABLE_URI)) {
                getContext().getContentResolver().notifyChange(PblLockerAppDataModel.TABLE_URI, null);
            } else if (uri.equals(PblLockerAppDataModel.TABLE_URI)) {
                getContext().getContentResolver().notifyChange(PblInstalledAppDataModel.TABLE_URI, null);
            }
        }
    }
}
